# -*- coding:utf-8 -*-

from redis_base import RedisBase
import time


class ForbiddenHelper(RedisBase):
    def __init__(self, counter, timer):
        super().__init__()
        self.counter = counter
        self.timer = timer

    def login_control(self, lid):
        key = "login:control:{}".format(lid)
        is_exists = self.conn.exists(key)
        if not is_exists:
            self.conn.set(key, 1, ex=self.timer)
            print("login is allowed")
            return True
        else:
            login_count = self.conn.get(key)
            if int(login_count) >= self.counter:
                print("login is forbidden")
                return False
            else:
                self.conn.incrby(key, 1)
                print("login is allowed")
                return True
